package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud.ques;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResSemester;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResSemesterRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 
 * @annotation: 云课程--查询教师关联学段学科版本册别
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class QuesCloudLessonCatalogSchController extends YxyApiBaseController {

    @Autowired
    private YxyResSubjectRepository yrsr;

    @Autowired
    private YxyResBookRepository yrbr;

    @Autowired
    private YxyResVersionRepository yrvr;

    @Autowired
    private YxyResSemesterRepository yxyResSemesterRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/ques/catalog/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        // 获取默认学段
        String phaseName = reqModel.phaseName;
        String phaseId = reqModel.phaseId;
        String subName = "";
        String subId = "";
        String verName = "";
        String verId = "";
        String semName = "";
        String semId = "";
        String bookId = "";
        // 获取学科版本册别
        List<String> listSubjectId = yrbr.findSubjectIdByPhaseIdGroupBySubjectId(phaseId);
        if (listSubjectId.size() > 0) {
            List<YxyResSubject> subjects = yrsr.findByIdInOrderByOrderNum(listSubjectId);
            subName = subjects.get(0).getName();
            subId = subjects.get(0).getId();
        }

        List<String> versionIds = yrbr.findVersionIdByPhaseIdAndSubjectIdGroupByVersionId(phaseId, subId);
        if (versionIds.size() > 0) {
            List<YxyResVersion> versions = yrvr.findByIdInAndStatusOrderByOrderNum(versionIds, 1);
            verName = versions.get(0).getName();
            verId = versions.get(0).getId();
        }

        List<YxyResBook> books = new ArrayList<YxyResBook>();
        books = yrbr.findBySubjectIdAndVersionIdAndPhaseIdAndStatus(subId, verId, phaseId, 1);
        if (books.size() > 0) {
            List<String> semesterIds = books.stream().map(x -> x.getSemersterId()).collect(Collectors.toList());
            List<YxyResSemester> yxyResSemesters = yxyResSemesterRepository
                    .findByIdInAndStatusOrderByOrderNum(semesterIds, 1);
            for (YxyResSemester yxyResSemester : yxyResSemesters) {
                YxyResBook yxyResType = books.stream()
                        .filter(x -> StringUtils.equals(x.getSemersterId(), yxyResSemester.getId())).findFirst().get();
                if (yxyResType != null) {
                    semId = yxyResType.getSemersterId();
                    semName = yxyResType.getSemersterName();
                    bookId = yxyResType.getId();
                }
                break;
            }
        }
        RespModel model = new RespModel();
        model.bookId = bookId;
        model.phaseId = phaseId;
        model.phaseName = phaseName;
        model.semId = semId;
        model.semName = semName;
        model.subId = subId;
        model.subName = subName;
        model.verId = verId;
        model.verName = verName;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phaseName;
        public String phaseId;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String phaseName;
        public String phaseId;
        public String subName;
        public String subId;
        public String verName;
        public String verId;
        public String semName;
        public String semId;
        public String bookId;
    }
}
